<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>学生端 - 师生学习助手</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <!-- 引入 layui 样式 -->
    <link rel="stylesheet" href="../static/layui/css/layui.css">
    <style>
        /* 页面背景 */
        body {
            background-color: #f4f4f4;
            height: 100vh; /* 使用视口高度 */
            margin: 0;
            display: flex;
            justify-content: center; /* 水平居中 */
            align-items: center; /* 垂直居中 */
            font-family: 'Arial', sans-serif;
        }

        .whole {
            width: 350px;
            padding: 40px;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 20px;
        }

        .input_tit {
            margin: 10px 0;
            font-weight: bold;
        }

        .input_box {
            margin-bottom: 20px;
        }

        .finish_btn {
            background-color: #1E9FFF;
            border: none;
            padding: 10px 20px;
            color: white;
            font-size: 16px;
            cursor: pointer;
            width: 100%;
            border-radius: 5px;
        }

        .finish_btn:hover {
            background-color: #1c87d9;
        }

        .input_self {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            border-radius: 5px;
            border: 1px solid #ccc;
        }

        .input_code {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            border-radius: 5px;
            border: 1px solid #ccc;
        }

        #imgCode {
            cursor: pointer;
            border-radius: 5px;
            height: 40px;
            width: 100px;
            margin-left: 10px; /* 给图片添加左边距，使输入框和图片有间隔 */
        }

        .finish_box {
            margin-top: 20px;
        }

        .input_box_code {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        /* 新增无账号提示样式 */
        .error-message {
            color: red;
            font-size: 14px;
            margin-top: 10px;
        }
    </style>
</head>

<body class="layui-bg-gray">
<div class="whole">
    <div class="title">学生端 - 师生学习助手</div>
    <form id="form1" class="layui-form">
        <div class="input_tit">学号</div>
        <div class="input_box">
            <input class="input_self layui-input" type="text" name="studentNumber" id="studentNumber" required placeholder="请输入学号" />
        </div>
        <div class="input_tit">密码</div>
        <div class="input_box">
            <input class="input_self layui-input" type="password" name="password" id="password" autocomplete="off" required placeholder="请输入密码" />
        </div>
        <div class="input_tit">验证码</div>
        <div class="input_box_code">
            <input class="input_code layui-input" type="text" name="Imgcode" id="Imgcode" required placeholder="请输入验证码" />
            <img alt="验证码" src="/login/verifyCode" id="imgCode" onclick="reloadCode()">
        </div>
        <div class="finish_box">
            <button type="button" class="finish_btn layui-btn layui-btn-normal" onclick="login()">登录</button>
        </div>
    </form>
</div>

<script type="text/javascript">
    $(function () {
        $(window).on('resize load', function () {
            var win_height = $(window).height();
            var whole_height = $(".whole").height();
            $(".whole").css('top', win_height / 2 - whole_height / 2 - 40 + 'px')
        })
    })

    // 更换验证码
    function reloadCode() {
        var time = new Date().getTime();
        document.getElementById("imgCode").src = "/login/verifyCode?id=" + time;
    }

    // 提交登录请求
    function login() {
        var studentNumber = $("#studentNumber").val();
        var password = $("#password").val();
        var Imgcode = $("#Imgcode").val();

        if (studentNumber === "" || studentNumber.length === 0) {
            alert("用户名为空");
        } else if (password === "" || password.length === 0) {
            alert("密码为空");
        } else if (Imgcode === "" || Imgcode.length === 0) {
            alert("验证码为空");
        } else {
            $.ajax({
                url: "/login/managerLogin",  // 地址
                type: "POST",  // 修改为 POST 请求
                contentType: "application/json",  // 设置请求体的格式为 JSON
                data: JSON.stringify({
                    studentNumber: studentNumber,
                    password: password,
                    imgcode: Imgcode  // 修改为与后端匹配的字段名（假设后端接收的是 imgcode）
                }),
                success: function (value) {
                    if (value.code === 200) {
                        // 登录成功，跳转到主页
                        window.location.href = '/index';
                    } else if (value.code === 444) {
                        alert("无账号，请联系管理员获取访问权限。");
                    } else if (value.code === 999) {
                        alert(value.message || "验证码错误");  // 显示验证码错误或默认提示
                    }
                },
                error: function (xhr, status, error) {
                    console.error("Error:", error);
                    alert("登录请求失败，请重试。");
                }
            });
        }
    }

</script>
</body>

</html>
